﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>出库</title>
    <script src="/Apm/js/CMSList.js"></script>
    <style>
        .list-form {
            padding: 3rem;
        }
        a {
            color: #01AAED;
        }
            a:hover {
                color: #FF5722;
            }
    </style>
</head>
<body>
    <div class="page">
        <div class="toolsbar">
            <button id="Add" class="layui-btn">
                <i class="layui-icon">&#xe654;</i> 添加
            </button>
            <button id="Delete" class="layui-btn layui-btn-danger">
                <i class="layui-icon">&#xe640;</i> 删除
            </button>
        </div>
        <div class="content">
            <table id="tabledata" lay-filter="datalist"></table>
        </div>
    </div>
</body>
    <div id="domSelect" style="display:none;" class="layui-form layui-form-pane list-form">
        <div class="layui-form-item">
            <label class="layui-form-label">仓库</label>
            <div class="layui-input-block">
                <select id="WareHouseGuidList" name="WareHouseGuid" required lay-verify="required" lay-filter="WareHouseGuidList">
                    <option value="">请选择仓库</option>

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">经销商</label>
            <div class="layui-input-block">
                <select id="CustomerGuidList" name="CustomerGuid" required lay-verify="required" lay-filter="CustomerGuidList">
                    <option value="">请选择经销商</option>

                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">出库时间</label>
            <div class="layui-input-block">
                <input id="Addtime" type="text" name="Addtime" required lay-verify="required" placeholder="请选择时间" autocomplete="off" class="layui-input">

            </div>
        </div>
    </div>

</html>
<script type="text/html" id="GuidView">
    <a id="{{d.Guid}}" onclick="ClickView(this.id)">{{d.Guid}}</a>
</script>
<script type="text/html" id="IsPrintTpl">
    <input type="checkbox" name="IsPrint" value="{{d.IsPrint}}" lay-skin="switch" disabled lay-text="是|否" {{ d.IsPrint == 1 ? 'checked' : '' }}>
</script>
<script>

    layui.use(['table', 'layer', 'element', 'laydate'], function () {
        var table = layui.table,
            layer = layui.layer,
            element = layui.element,
            laydate = layui.laydate;

        BindData();
        GetKHList();
        GetCKList();
        //新增
        $("#Add").click(function () {

            //弹层选择采购时间
            layer.open({
                type: 1,
                title: '选择',
                content: $("#domSelect"),
                success: function (layero, index) {
                    //执行一个laydate实例
                    laydate.render({
                        elem: '#Addtime' //指定元素
                    });
                    $("#Addtime").val('');
                    $("#WareHouseGuidList").val('');
                    $("#CustomerGuidList").val('');
                },
                btn: ['确定', '关闭'],
                yes: function (index, layero) {
                    var time = $("#Addtime").val();
                    if (time == "") {
                        layer.msg('请选择时间');
                    }
                    else {
                        var WareHouseGuid = $("#WareHouseGuidList").val();
                        var CustomerGuid = $("#CustomerGuidList").val();
                        if (WareHouseGuid == "") {
                            layer.msg('请选择仓库');
                            return;
                        }
                        if (CustomerGuid == "") {
                            layer.msg('请选择经销商');
                            return;
                        }
                        var json = new Object();
                        json.Addtime = time;
                        json.WareHouseGuid = WareHouseGuid;
                        json.CustomerGuid = CustomerGuid;
                        ajaxpost({
                            url: 'api/FriendFlower/OrderOutboundCreate?Token=' + MyPublic.getToken(),
                            data: json,
                            success: function (data) {
                                data = JSON.parse(data);
                                if (data.code === "0") {
                                    
                                    layer.close(index);
                                    var guid = data.data.Guid;
                                    //填写详单
                                    layer.open({
                                        title: '出库',
                                        type: 2,
                                        area: ['700px', '80%'],
                                        content: '/FlowerManage/Outbound/OutboundInfoEdit.html?type=add&OutboundGuid=' + guid,
                                        cancel: function () {

                                            //新增点击关闭时，删新增的
                                            ajaxget({
                                                url: 'api/FriendFlower/OrderOutboundDelete?Token=' + MyPublic.getToken() + '&Guid=' + guid,
                                                success: function (data) {
                                                    data = JSON.parse(data);
                                                    
                                                }
                                            })
                                            
                                        },
                                        end: function () {
                                            BindData();
                                        }
                                    });
                                }
                                else {
                                    layer.msg(data.msg);
                                }
                            }
                        })
                        


                    }

                }
            });

        })
        
        //删除
        $("#Delete").click(function () {
            var checkStatus = table.checkStatus('datalist');

            var rows = checkStatus.data.length;
            if (rows > 0) {
                if (rows == 1) {
                    var id = checkStatus.data[0].Guid;
                    var state = checkStatus.data[0].IsPrint;
                    if (state === 1) {
                        layer.msg("已打印的出库单不能被删除！");
                    }
                    else {
                        layer.confirm('确定删除？', { icon: 3, title: '删除' }, function (index) {

                            ajaxget({
                                url: 'api/FriendFlower/OrderOutboundDelete?Token=' + MyPublic.getToken() + '&Guid=' + id,
                                success: function (data) {
                                    data = JSON.parse(data);

                                    if (data.code === "10002") {
                                        layer.close(index);
                                        BindData();
                                    }
                                    else {
                                        layer.msg(data.msg);
                                    }
                                }
                            })


                        });
                    }

                    
                }
                else {
                    layer.msg("此模块不支持删除多条记录");
                }
            }
            else {
                layer.msg("请选择要删除的记录");
            }
        });
       
    });
    /**
     * 打开查看页面
     * @param id
     */
    function ClickView(id) {
        parent.AddTab('View' + id, '/FlowerManage/Outbound/OutboundInfoView.html?guid=' + id, '&#xe6f3;', '出库单');
    }
    /**
     * 经销商数据绑定
     */
    function GetKHList() {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;

            ajaxget({
                url: 'api/FriendFlower/CustomerList?Token=' + MyPublic.getToken(),
                success: function (data) {
                    var lis = [];
                    layui.each(data, function (index, item) {
                        lis.push('<option value="' + item.Guid + '">' + item.Name + '</option>');
                    })
                    var datahtml = lis.join('');

                    $("#CustomerGuidList").append(datahtml);
                    form.render('select');

                }
            })
        });
    }
    /**
     * 仓库数据绑定
     */
    function GetCKList() {
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer,
                form = layui.form;

            ajaxget({
                url: 'api/FriendFlower/WareHouseList?Token=' + MyPublic.getToken(),
                success: function (data) {
                    var lis = [];
                    layui.each(data, function (index, item) {
                        lis.push('<option value="' + item.Guid + '">' + item.Name + '</option>');
                    })
                    var datahtml = lis.join('');

                    $("#WareHouseGuidList").append(datahtml);
                    form.render('select');
                    
                }
            })
        });
    }
    /**
     * 初始化数据
     */
    function BindData() {
        layui.use(['table', 'layer'], function () {
            var table = layui.table,
                layer = layui.layer;

            ajaxget({
                url: 'api/FriendFlower/GetOrderOutboundList?Token=' + MyPublic.getToken(),
                success: function (data) {

                    table.render({
                        id: 'datalist',
                        elem: '#tabledata',
                        cols: [[//标题栏
                            { type: 'checkbox' },
                            { field: 'Guid', title: '出库单号', align: 'center', width: 180, templet: '#GuidView' },
                            { field: 'Title', title: '出库单标题', align: 'center' },
                            { field: 'CustomerName', title: '经销商', align: 'center' },
                            { field: 'WareHouseName', title: '仓库', align: 'center' },
                            { field: 'Ys', title: '应收款', align: 'right', minWidth: 100 },
                            { field: 'Ss', title: '实收款', align: 'right', minWidth: 100 },
                            { field: 'Ws', title: '未收款', align: 'right', minWidth: 100 },
                            { field: 'AddName', title: '出库人', align: 'center' },
                            { field: 'IsPrint', title: '是否打印', align: 'center', templet: '#IsPrintTpl' }
                        ]],
                        data: data,
                        height: 'full-68',
                        limit: 20,
                        limits: [20, 40, 60],
                        page: true
                    })
                }
            })

        });
    }
</script>